<?php

	$TEMP->set_file(array('handle' => DIR_IFRAME_VIEW . 'personalInformation.html'));
	
	if ( isset($_POST['subButton']) ) {
		
		$if_error		= 0;
		$if_display		= 0;
		$txtfname		= _POST('txtfname');
		$txtmname		= _POST('txtmname'); 
		$txtlname		= _POST('txtlname');
		$txtgender		= _POST('txtgender');
		$txtbdatemm		= _POST('txtbdatemm');	
		$txtbdatedd		= _POST('txtbdatedd'); 
		$txtbdateyy		= _POST('txtbdateyy');
			$txtdate	= date("Y-m-d", strtotime($txtbdateyy . '-' .$txtbdatemm . '-' . $txtbdatedd));
		$txtemail		= _POST('txtemail');
		$txtcontact		= _POST('txtcontact');
		$txtaddress		= _POST('txtaddress');
		$txtguardian	= _POST('txtguardian');
		
		if ( empty($txtfname) )		{ $if_error++; $_txtfname 	= WARNING_INPUT; }
		if ( empty($txtmname) )		{ $if_error++; $_txtmname 	= WARNING_INPUT; }
		if ( empty($txtlname) )		{ $if_error++; $_txtlname 	= WARNING_INPUT; }
		
		if ( empty($txtgender) )	{ $if_error++; $_txtgender 		= WARNING_INPUT; }
		
		if ( !empty($txtbdatemm) || !empty($txtbdatedd) || !empty($txtbdateyy) ) {
			if ( $txtdate == '1970-01-01' || $txtdate == '0000-00-00')	{ $if_error++; $_txtdate 	= WARNING_INPUT; }
		}
		
		if ( empty($txtcontact) )	{ $if_error++; $_txtcontact 	= WARNING_INPUT; }
		if ( empty($txtaddress) )	{ $if_error++; $_txtaddress 	= WARNING_INPUT; }
		
		if ( $if_error == 0 ) {

			
			$strArray = array('first_name' 			=> firstUpperCase($txtfname),
							  'last_name' 			=> firstUpperCase($txtlname),
							  'middle_name' 		=> firstUpperCase($txtmname),
							  'birthdate' 			=> $txtdate,
							  'gender' 				=> $txtgender,
							  'email' 				=> $txtemail,
							  'contact_no' 			=> $txtcontact,
							  'address' 			=> firstUpperCase($txtaddress),
							  'guardian' 			=> firstUpperCase($txtguardian));
			
			$PI->updateStudentPersonalInformation($strArray, $logUserID);
			$saved_record = "Saved";
			
		}
		else {
			$TEMP->set_var(array('w_txtfname' 		=> $_txtfname,
								 'w_txtmname' 		=> $_txtmname,
								 'w_txtlname' 		=> $_txtlname,
								 'w_txtgender' 		=> $_txtgender,
								 'w_txtbdatemm'		=> $_txtdate,
								 'w_txtbdatedd'		=> $_txtdate,
								 'w_txtbdateyy'		=> $_txtdate,
								 'w_txtemail'		=> $_txtemail,
								 'w_txtcontact'		=> $_txtcontact,
								 'w_txtaddress'		=> $_txtaddress,
								 ));
		}
	}
	else {
		
		$personalInfo = $PI->getStudentPersonalInformation($logUserID);
		
		$txtfname 		= $personalInfo['first_name'];
		$txtlname 		= $personalInfo['last_name'];
		$txtmname 		= $personalInfo['middle_name'];
		
		$birthdate 		= $personalInfo['birthdate'];
			$txtbdatemm	= m($birthdate);	
			$txtbdatedd	= d($birthdate); 
			$txtbdateyy	= y($birthdate);
		
		$txtgender 		= $personalInfo['gender'];
		$txtemail 		= $personalInfo['email'];
		$txtcontact 	= $personalInfo['contact_no'];
		$txtaddress 	= $personalInfo['address'];
		$txtguardian 	= $personalInfo['guardian'];
		$txtusername 	= $personalInfo['log_user'];
		
	}
	
	$TEMP->set_var(array('txtfname' 			=> $txtfname,
						 'txtmname' 			=> $txtmname,
						 'txtlname' 			=> $txtlname,
						 'selected'.$txtgender 	=> 'selected',
						 'selected'.$txtbdatemm	=> 'selected',
						 'txtbdatedd'			=> $txtbdatedd,
						 'txtbdateyy'			=> $txtbdateyy,
						 'txtemail'				=> $txtemail,
						 'txtcontact'			=> $txtcontact,
						 'txtaddress'			=> $txtaddress,
						 'txtguardian'			=> $txtguardian,
						 'txtusername'			=> $txtusername,
						 'saved_record'			=> $saved_record
						 ));
?>